package Dao;

import DBTool.DB;
import bean.Car;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

public class selectDao {
    public static ArrayList<Car> getAllcar() {
        ArrayList<Car> list = new ArrayList<>();
        String sql = "select * from brand b,cardetail c where b.BrandID=c.BrandID";
        ResultSet rs = DB.query(sql);
        try {
            while (rs.next()) {
                int cid = rs.getInt(1);
                String brandname = rs.getString(2);
                String cname = rs.getString(4);
                String content = rs.getString(5);
                int price = rs.getInt(7);
                Date time = rs.getDate(6);
                Car car = new Car(cid, brandname, cname, content, price, time);
                list.add(car);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public static int add(Car car) {
        String sql = "insert into cardetail values(?,?,?,?,?,?)";
        int num = DB.update(sql, car.getcId(), car.getbName(), car.getcName(), car.getContent(), car.getPrice(), car.getTime());
        return num;
    }
}
